package{
	import com.PQLoad.PQLoader;
	import com.tools.EventHelper;
	import com.tools.PQDrawTools;
	import com.tools.Tip;
	import com.tools.UIHelper;
	
	import flash.desktop.ClipboardFormats;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.NativeDragEvent;
	import flash.net.URLRequest;
	import flash.utils.unescapeMultiByte;

	public class SceneView extends Sprite{
		public var bg:Sprite;
		public var panel:Sprite;
		/**当前处理的对象**/
		private var curObj:Mc;
		public function SceneView(){
			super();
			addEventListener(Event.ADDED_TO_STAGE,addToStage);
		}
		private function addToStage(e:Event):void{
			bg=PQDrawTools.drawRect(0,0,stage.stageWidth,stage.stageHeight,1,0x000000,0xCCCCCC,1);
			addChild(bg);
			panel=new Sprite();
			addChild(panel);
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN,keyChange);
			EventHelper.self.addListen(EventType.ADD_MC,addMc);
			EventHelper.self.addListen(EventType.ADD_TXT,addTxt);
			EventHelper.self.addListen(EventType.SET_TOP,setTop);
			EventHelper.self.addListen(EventType.SHOW_ALL,showAll);
			EventHelper.self.addListen(EventType.CLEAR,clear);
			EventHelper.self.addListen(EventType.SUBMIT,submit);
			EventHelper.self.addListen(EventType.REMOVE,remove);
			EventHelper.self.addListen(EventType.CREATE_VIEW,createView);
		}
		private function addMc(type:String,obj:Object=null):void{
			Tip.showTip("功能未实现");
		}
		private function addTxt(type:String,obj:Object=null):void{
			var mc:Mc=new Mc();
			mc.addChild(UIHelper.self.createTxt({text:"新文本",width:60,height:20,size:10,border:1}));
			mc.x=200;
			mc.y=100;
			mc.mouseChildren=false;
			setResEvent(mc);
			panel.addChild(mc);
		}
		private function setTop(type:String,obj:Object=null):void{
			if(!curObj)return;
			curObj.parent.addChild(curObj);
		}
		private function showAll(type:String,obj:Object=null):void{
			for(var i:int=0;i<panel.numChildren;i++){
				panel.getChildAt(i).visible=true;
				if(panel.getChildAt(i).alpha==0)panel.getChildAt(i).alpha=1;
			}
		}
		private function clear(type:String,obj:Object=null):void{
			panel.removeChildren();
		}
		private function submit(type:String,obj:Object=null):void{
			OutputParsing.traceConfig(panel);
		}
		private function remove(type:String,obj:Object=null):void{
			if(!curObj||!curObj.parent)return;
			curObj.parent.removeChild(curObj);
		}
		public function createView(type:String,configList:Array):void{
			for each(var obj:Object in configList){
				var p:Mc=new Mc();
				var img:DisplayObject;
				if(obj.res&&obj.res!="null"){
					if(obj.res.substr(obj.res.length-4).toLocaleLowerCase()==".png")img=PQLoader.getPng(obj.res);
					else img=PQLoader.getImg(obj.res);
				}else{
					img=UIHelper.self.createTxt({text:"新文本",width:obj.width,height:obj.height,size:10,border:1})
				}
				p.addChild(img);
				p.mouseChildren=false;
				p.buttonMode=true;
				p.name=obj.name;
				p.url=obj.res;
				p.x=obj.x;
				p.y=obj.y;
				p.alpha=obj.alpha;
				p.width=obj.width;
				p.height=obj.height;
				p.scaleX=obj.scaleX;
				p.scaleY=obj.scaleY;
				p.parentName=obj.parent;
				p.rotation=obj.rotation;
				p.type=obj.type;
				setResEvent(p);
				panel.addChild(p);
			}
		}
		public function update():void{
			bg.graphics.clear();
			bg.graphics.beginFill(0xcccccc);
			bg.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
			bg.graphics.endFill();
			bg.graphics.lineStyle(1,0x000000);
			bg.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
		}
		/**载入拖入的资源**/
		public function load(event:NativeDragEvent):void {
			var files:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
			var n:int=files.length;
			this.doubleClickEnabled=true;
			for(var i:int=0;i<n;i++){
				if(files[i].url.substr(files[i].url.length-3)=="swf"&&Config.self.className!="类名"){
					PQLoader.load(files[i].url,{onComplete:loadswf,onCompleteParam:files[i].url});
				}else{
					var mc:Mc=new Mc();
					mc.name="resName_"+Math.round(Math.random()*1000);
					mc.url=unescapeMultiByte(files[i].url);
					var loader:Loader=new Loader();
					loader.load(new URLRequest(files[i].url));
					mc.buttonMode=true;
					mc.mouseChildren=false;
					setResEvent(mc);
					mc.x=this.stage.stageWidth/2-100;
					mc.y=this.stage.stageHeight/2-50;
					panel.addChild(mc);
					mc.addChild(loader);
				}
			}
		} 
		/**载入拖入的SWF资源**/
		private function loadswf(...args):void{
			var mc:Sprite=new Sprite();
			mc.name=args[1];
			mc.buttonMode=true;
			mc.mouseChildren=false;
			setResEvent(mc);
			stage.addEventListener(MouseEvent.MOUSE_UP,sdrag);
			var cl:Class=PQLoader.getClassByLibrary(Config.self.className,args[1]) as Class;
			mc.addChild(new cl());
			panel.addChild(mc);
			mc.x=stage.stageWidth/2;
			mc.y=stage.stageHeight/2;
		}
		private function setResEvent(mc:Sprite):void{
			mc.addEventListener(MouseEvent.MOUSE_DOWN,drag);
			mc.addEventListener(MouseEvent.MOUSE_UP,sdrag);
			mc.addEventListener(MouseEvent.CLICK,resClick);
		}
		private function resClick(e:MouseEvent):void{
			curObj=e.currentTarget as Mc;
			EventHelper.self.call(EventType.BIND_MC,e.currentTarget);
		}
		/**拖动对象**/
		private function drag(e:MouseEvent):void{
			curObj=e.currentTarget as Mc;
			e.currentTarget.startDrag();
		}
		/**停止拖动**/
		private function sdrag(e:MouseEvent):void{
			if(curObj)curObj["stopDrag"]();
			if(mouseX<0||mouseX>stage.stageWidth)curObj.parent.removeChild(curObj);
			else if(mouseY<0||mouseY>stage.stageHeight)curObj.parent.removeChild(curObj);
		}
		/**键盘操作**/
		private function keyChange(e:KeyboardEvent):void{
			if(!curObj)return;
			var update:Boolean=false;
			if(e.keyCode==37){//左
				curObj.x-=1;
				update=true;
			}else if(e.keyCode==38){//上
				curObj.y-=1;
				update=true;
			}else if(e.keyCode==39){//右
				curObj.x+=1;
				update=true;
			}else if(e.keyCode==40){//下
				curObj.y+=1;
				update=true;
			}else if(e.keyCode==16){//shift
				panel.addChild(curObj);
			}
			if(update)EventHelper.self.call(EventType.BIND_MC,curObj);
		}
	}
}